// Copyright 2011 Jay Young. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview
 *
 * A handler class for the Relieved Blog home page.
 */

goog.provide('rb.handlers.LandingHandler');

goog.require('rb.post.EventType');
goog.require('rb.post.GetPostCommand');
goog.require('rb.post.PostEvent');

goog.require('relief.nav.Handler');



/**
 * A handler class for the Relieved blog home page.
 *
 * @param {rb.ServiceProvider} sp The ServiceProvider.
 * @constructor
 * @implements {relief.nav.Handler}
 */
rb.handlers.LandingHandler = function(sp) {
  /**
   * @type {rb.ServiceProvider}
   * @private
   */
  this.sp_ = sp;
};


/**
 * @inheritDoc
 */
rb.handlers.LandingHandler.prototype.handle = function(path) {
  var callback = goog.bind(this.gotPost_, this),
      cmd = new rb.post.GetPostCommand(callback, callback);

  this.sp_.getRPCService().execute(cmd);
};


/**
 * @inheritDoc
 */
rb.handlers.LandingHandler.prototype.transition = function(path, onTransition) {
  this.handle(path);
  onTransition(true);
};


/**
 * @inheritDoc
 */
rb.handlers.LandingHandler.prototype.exit = function(onExit) {
  onExit(true);
};


/**
 * Got the post, or an error.  Display it.
 *
 * @param {(rb.post.BlogPost|rb.post.GetPostCommand.FailureCode)} resp The blog
 *    post to display, or the failure code if the request did not get a post.
 * @private
 */
rb.handlers.LandingHandler.prototype.gotPost_ = function(resp) {
  var content = this.sp_.getContentRoot();

  if (resp instanceof rb.post.BlogPost) {
    var post = resp;

    content.innerHTML = rb.post.templates.showPost({
      title: post.getTitle(),
      content: post.getContent(),
      created: post.getCreatedForDisplay(),
      author: post.getAuthor().displayName
    });

    var eventBus = this.sp_.getEventBus(),
        e = new rb.post.PostEvent(rb.post.EventType.DISPLAY, post);
    eventBus.dispatchEvent(e);
  }
  else if (resp === rb.post.GetPostCommand.FailureCode.NO_POSTS) {
    content.innerHTML = relief.handlers.templates.genericError({
      header: 'No Posts Yet',
      details: 'No one has created any blog posts yet.  Stay tuned!'
    });
  }
  else {
    content.innerHTML = relief.handlers.templates.genericError({
      header: 'Error Retrieving Post',
      details: 'There was an error retrieving the requested post.  ' +
          'Please try again later.'
    });
  }
};


/**
 * @inheritDoc
 */
rb.handlers.LandingHandler.prototype.dispose = function() {
  this.sp_ = null;
};


/**
 * @inheritDoc
 */
rb.handlers.LandingHandler.prototype.isDisposed = function() {
  return this.sp_ === null;
};
